home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 April: Mac OS SDK / Dev.CD Apr 96 SDK / Dev.CD Apr 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc Development Framework / ODFDev / ODF / Found / FWMemory / Sources / FWMemTas.cpp < prev   
Encoding:
Text File  |  1995-11-08  |  5.5 KB  |  189 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWMemTas.cpp
  4. //    Release Version:    $ 1.0d11 $
  5. //
  6. //    Copyright:    (c) 1993, 1995 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWFound.hpp"
  11.  
  12. #ifndef   FWMEMTAS_H
  13. #include "FWMemTas.h"
  14. #endif
  15.  
  16. // ----- Foundation Includes -----
  17.  
  18. #ifndef FWEXCDEF_H
  19. #include "FWExcDef.h"
  20. #endif
  21.  
  22. #ifndef FWEXCTAS_H
  23. #include "FWExcTas.h"
  24. #endif
  25.  
  26. #ifndef FWPRIDEB_H
  27. #include "FWPriDeb.h"
  28. #endif
  29.  
  30. #ifndef FWNEW_H
  31. #include "FWNew.h"
  32. #endif
  33.  
  34. // ----- OpenDoc Includes -----
  35.  
  36. #if !defined(FW_qUsePlatformAlloc) && !defined(FW_qUseCRuntimeAlloc)
  37.  
  38. #ifdef FW_BUILD_WIN
  39. #include <MMStubs.h>
  40. #endif
  41.  
  42. #ifdef FW_BUILD_MAC
  43. #include <MemMgr.h>
  44. #endif
  45.  
  46. #endif
  47.  
  48. // ----- Platform Includes -----
  49.  
  50. #include <stdlib.h>
  51.  
  52. #if FW_LIB_EXPORT_PRAGMAS
  53. #pragma lib_export on
  54. #endif
  55.  
  56. #ifdef FW_BUILD_MAC
  57. #pragma segment FWMemory_TaskGlobals
  58. #endif
  59.  
  60. //----------------------------------------------------------------------------------------
  61. //    FW_CMemoryTaskGlobals::GetMemoryGlobals
  62. //----------------------------------------------------------------------------------------
  63.  
  64. FW_SPrivMemoryGlobals& FW_CMemoryTaskGlobals::GetMemoryGlobals()
  65. {
  66. //    FW_SPrivMemoryGlobals *globals = (FW_SPrivMemoryGlobals*)
  67. //                FW_CPrivTaskGlobals::GetTaskGlobals(kMemoryGlobalsOffset);
  68.     FW_SPrivMemoryGlobals *globals = &gGlobals;
  69.     FW_PRIV_ASSERT(globals != 0);
  70. #ifdef FW_qUsePlatformAlloc
  71.     if (globals->gInitialized == 0)
  72.         Initialize(*globals);
  73. #else
  74.     if (globals->gMemoryHeap == 0)
  75.         Initialize(*globals);
  76. #endif
  77.     return *globals;
  78. }
  79.  
  80. //----------------------------------------------------------------------------------------
  81. // Memory allocation glue code
  82. //----------------------------------------------------------------------------------------
  83. static void* OperatorNewHandler(size_t size)
  84. {
  85. #if defined(FW_qUsePlatformAlloc) || defined(FW_qUseCRuntimeAlloc)
  86.     return ::malloc(size);
  87. #else
  88.     return ::MMAllocateIn(size, FW_CMemoryTaskGlobals::GetMemoryGlobals().gMemoryHeap);
  89. #endif
  90. }
  91.  
  92. static void OperatorDeleteHandler(void* p)
  93. {
  94. #if defined(FW_qUsePlatformAlloc) || defined(FW_qUseCRuntimeAlloc)
  95.     ::free(p);
  96. #else
  97.     ::MMFree(p);
  98. #endif
  99. }
  100.  
  101. //----------------------------------------------------------------------------------------
  102. // FW_CMemoryTaskGlobals::gGlobals
  103. //----------------------------------------------------------------------------------------
  104.  
  105. FW_SPrivMemoryGlobals FW_CMemoryTaskGlobals::gGlobals;
  106.  
  107. //----------------------------------------------------------------------------------------
  108. // FW_CMemoryTaskGlobals::Initialize()
  109. //----------------------------------------------------------------------------------------
  110.  
  111. void FW_CMemoryTaskGlobals::Initialize(FW_SPrivMemoryGlobals& memGlobals)
  112. {
  113.     memGlobals.gMemoryHeap = 0;
  114.     InitializeMemoryHeap(memGlobals);
  115.     memGlobals.gLastRequested = 0;
  116.     memGlobals.gNewHandler = FW_CMemoryManager::DefaultNewHandler;
  117.  
  118.     ::set_new_handler(memGlobals.gNewHandler);
  119.  
  120.     FW_SPrivExceptionGlobals &excGlobals = FW_CExceptionTaskGlobals::GetExceptionGlobals();
  121.  
  122. #if !defined(FW_qUsePlatformAlloc) && !defined(FW_qUseCRuntimeAlloc)
  123.     if (excGlobals.gOperatorNewHandler == 0)
  124.     {
  125.         excGlobals.gOperatorNewHandler = &::operator new;
  126.         excGlobals.gOperatorDeleteHandler = &::operator delete;
  127.     }
  128.     
  129.     // Note: Local variables are used here because it illegal to use the address of an
  130.     // overloaded function in an expression, as in the asserts below. See ARM pg. 327
  131.     // for legal uses of the address of an overloaded function.
  132.     
  133.     __FW_OperatorNewHandler newHandler = &::operator new;
  134.     __FW_OperatorDeleteHandler deleteHandler = &::operator delete;
  135.     
  136.     FW_ASSERT(excGlobals.gOperatorNewHandler == newHandler);
  137.     FW_ASSERT(excGlobals.gOperatorDeleteHandler == deleteHandler);
  138. #endif
  139. }
  140.  
  141. //----------------------------------------------------------------------------------------
  142. // FW_CMemoryTaskGlobals::Terminate()
  143. //----------------------------------------------------------------------------------------
  144.  
  145. void FW_CMemoryTaskGlobals::Terminate()
  146. {
  147.     TerminateMemoryHeap();
  148. }
  149.  
  150.  
  151. //----------------------------------------------------------------------------------------
  152. // FW_CMemoryTaskGlobals::SetMemoryHeap()
  153. //----------------------------------------------------------------------------------------
  154.  
  155. void FW_CMemoryTaskGlobals::SetMemoryHeap(MemHeap *aMemoryHeap)
  156. {
  157.     GetMemoryGlobals().gMemoryHeap = aMemoryHeap;
  158. }
  159.  
  160. //----------------------------------------------------------------------------------------
  161. // FW_CMemoryTaskGlobals::InitializeMemoryHeap()
  162. //----------------------------------------------------------------------------------------
  163.  
  164. void FW_CMemoryTaskGlobals::InitializeMemoryHeap(FW_SPrivMemoryGlobals& memGlobals)
  165. {
  166.     memGlobals.gInitialized = 1;
  167.  
  168. #if defined(FW_qUsePlatformAlloc) || defined(FW_qUseCRuntimeAlloc)
  169.     memGlobals.gMemoryHeap = NULL;
  170. #else
  171.     memGlobals.gMemoryHeap = ::MMGetDefaultHeap();
  172. #ifdef FW_DEBUG
  173.     // Check with Jens about what makes sense here. [AMB]
  174. #endif
  175. #endif
  176. }
  177.  
  178. //----------------------------------------------------------------------------------------
  179. // FW_CMemoryTaskGlobals::TerminateMemoryHeap()
  180. //----------------------------------------------------------------------------------------
  181. void FW_CMemoryTaskGlobals::TerminateMemoryHeap()
  182. {
  183. #if !defined(FW_qUsePlatformAlloc) && !defined(FW_qUseCRuntimeAlloc)
  184.     FW_SPrivMemoryGlobals& memGlobals = GetMemoryGlobals();
  185.     // OpenDoc will delete the default heap, so just set it NULL.
  186.     memGlobals.gMemoryHeap = NULL;
  187. #endif
  188. }
  189.